<?php

namespace App\Http\Controllers\admin;

use App\Http\Requests\ClassifyRequest;
use App\Model\Classify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class ClassifyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $item = Classify::orderBy('id','asc')->paginate(15);
        $two_level = Classify::where([['two_level','!=',0]])->get();

        return view('admin.news.classify-list',compact('item','two_level'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = Classify::where('two_level',0)->get();
        $classify = Classify::where([['two_level','!=',0]])->get();
        $item = array();
        foreach ($classify as $value){
            foreach ($data as $v) {
                if ($v->id == $value->two_level){
                    $item[] = $value;
                }
            }
        }
        return view('admin.news.classify-add',compact('data','classify'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ClassifyRequest $request)
    {
        $m = new Classify();
        $m->title = $request->title;

        if ($request->input('articlecolumn') != 0){
            if ($request->input('articlecolumn') == 100){
                return $this->error('当前只能创建二级菜单，请重新选择吧！');
            }
            $m->two_level = $request->input('articlecolumn');
        }
        $m->order = $request->input('order') ? $request->input('order') : 0;
        $m->is_display = $request->input('inlineRadioOptions');
        $m->keywords = $request->input('keywords');
        $m->description = $request->input('abstract');
        if ($m->save()){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Classify::where('two_level',0)->get();
        $classify = Classify::where([['two_level','!=',0]])->get();
        $item = array();
        foreach ($classify as $value){
            foreach ($data as $v) {
                if ($v->id == $value->two_level){
                    $item[] = $value;
                }
            }
        }
        $res = Classify::where('id',$id)->first();
        return view('admin.news.classify-edit',['data'=>$data,'item'=>$item,'res'=>$res]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required'
        ],[
            'title.required' => '请填写文章分类'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }
        $m = Classify::find($id);
        $m->title = $request->input('articletitle');
        if ($request->input('articlecolumn') != 0){
            if ($request->input('articlecolumn') == 100){
                return $this->error('当前只能创建二级菜单，请重新选择吧！');
            }
            $m->two_level = $request->input('first-level');
        }
        $m->order = $request->input('articlesort');
        $m->is_display = $request->input('inlineRadioOptions');
        $m->keywords = $request->input('keywords');
        $m->description = $request->input('abstract');
        if ($m->save()){
            return $this->success('更新成功');
        }else{
            return $this->error('更新失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Classify::where('id',$id)->first();
        if ($data->top_level != null){
            $is_have = Classify::where('two_level',$data->id)->get();
            $dataArr = json_decode($is_have);
            if (empty($dataArr)){
                $res = Classify::where('id',$id)->delete();
                if ($res){
                    return $this->success('删除成功');
                }else{
                    return $this->error('删除失败');
                }
            }else{
                return $this->error('当前栏目下还有二级栏目，不能删除！');
            }
        }else{
            $res = Classify::where('id',$id)->delete();
            if ($res){
                return $this->success('删除成功');
            }else{
                return $this->error('删除失败');
            }
        }
    }
}
